﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super.Infrastructure;
using Super.Entities;
using System.Web.Mvc;

namespace Super.DomainService
{
    public class ProductCategoryService:BaseService<ProductCategory>
    {
        private readonly DbContextAdapter _adp;
        private readonly BaseService<ProductCategory> _categoryService;

        public ProductCategoryService(DbContextAdapter adapter) : base(adapter)
        {
            _adp = adapter;
            _categoryService = new BaseService<ProductCategory>(_adp);
        }

        /// <summary>
        /// 根据ID获取一个Category实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ProductCategory getCategoryByID(int ID)
        {
            return _categoryService.Repository.Find(a => a.ID == ID & a.IsDel==false).FirstOrDefault();
        }

        /// <summary>
        /// 获取订购产品的分类
        /// </summary>
        /// <returns></returns>
        public List<ProductCategory> getDingGouCategory()
        {
            return _categoryService.Repository.Find(a => a.DGProduct.Count > 0).OrderBy(a=>a.Sort).ToList();
        }

        /// <summary>
        /// 检测产品分类名称是否可用（是否存在重复）
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public object validateCategory(string categoryName)
        {
            var shopCategoryModel = _categoryService.Repository.Find(a => a.Name==categoryName & a.IsDel == false).FirstOrDefault();
            if (shopCategoryModel != null)
            {
                return "已存在的产品分类名称";
            }
            else
            {
                return true;
            }
        }

    
        /// <summary>
        /// 删除产品分类信息。isdel为true表示从数据库删除该记录；isdel默认为false，表示将IsDel设为1
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isdel"></param>
        /// <returns></returns>
        public void delCategory(int id, bool isdel = false)
        {
            var model = _categoryService.Repository.Find(a => a.ID == id & a.IsDel == false).FirstOrDefault();
            //真删
            if (isdel)
            {
                _categoryService.Repository.Delete(model);  
            }
            //假删
            else
            {
                model.IsDel = true;
                _categoryService.Repository.Update(model);
            }
        }



    }
}
